package com.example.execise1.service.impl;

import com.example.execise1.mapper.HeroMapper;
import com.example.execise1.service.HeroService;
import com.example.execise1.dto.HeroInfo;
import com.example.execise1.dto.HeroTypeData;
import com.example.execise1.dto.SkinInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("HeroService")
public class HeroServiceImpl implements HeroService {
    //使用构造器注入的方式，注入Mapper
    private final HeroMapper heroMapper;
    public HeroServiceImpl(HeroMapper heroMapper) {
        this.heroMapper = heroMapper;
    }

    //创建一个皮肤数据功能
    @Override
    public SkinInfo createSkin(SkinInfo skinInfo) {
        Integer rowCount = heroMapper.createSkin(skinInfo);
        if (rowCount >0){
            return skinInfo;
        }
        return null;
    }

    //更新指定英雄的次要分类API
    @Override
    public HeroInfo updateHeroType(Integer id, Integer newTypeId) {
        Integer rowCount = heroMapper.updateHeroType(id, newTypeId);
        if (rowCount > 0){
            return heroMapper.getHeroInfo(id);
        }
        return null;
    }

    //统计各类型英雄数量API，按类型id升序排序
    @Override
    public List<HeroTypeData> getHeroCount() {
        return heroMapper.getHeroCount();
    }

    //按分类名称查询英雄列表API，分页返回
    @Override
    public Page<HeroInfo> getTypeName(String typeName, Integer page, Integer size) {
        //英雄名称列表
        List<HeroInfo> typeNameDataList = heroMapper.getTypeName(typeName, page, size);
        //记录总数
        Long totalCount = heroMapper.getHeroTypeCount(typeName);
        //列表+分页信息+记录总数 产生分页对象
        return new PageImpl<>(typeNameDataList, PageRequest.of(page,size),totalCount);
    }

    //随机选取五个英雄组成一个队伍
    //获取一个战士
    @Override
    public HeroInfo getTank() {
        return heroMapper.getTank();
    }
    //获取一个法师
    @Override
    public HeroInfo getMage() {
        return heroMapper.getMage();
    }
    //获取一个射手
    @Override
    public HeroInfo getStriker() {
        return heroMapper.getStriker();
    }
    //获取一个辅助
    @Override
    public HeroInfo getAuxiliary() {
        return heroMapper.getAuxiliary();
    }
    //获取一个刺客
    @Override
    public HeroInfo getAssassin() {
        return heroMapper.getAssassin();
    }
}